package com.kangtushizhe.controller;

import com.kangtushizhe.emuns.Cart;
import com.kangtushizhe.emuns.Drug;
import com.kangtushizhe.entities.CommonResult;
import com.kangtushizhe.service.DrugService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-23
 */
@RestController
@RequestMapping("/drug")
@Tag(name = "药物")
public class DrugController {
    @Resource
    private DrugService drugService;

    //获取所有药物
    @GetMapping("/all")
    @Operation(summary = "获取所有药物", description = "() => 获取所有药物")
    public CommonResult getAllDrug() {
        List<Drug> allDrugs = drugService.list();
        return new CommonResult(200, "获取所有药物", allDrugs);
    }

    //创建新药物
    @PostMapping("/")
    @Operation(summary = "创建新药物", description = "(String position, Integer number, String validity,\n" +
            "                                   Integer isPrescription, String wDescribe) => 创建新药物")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "position", description = "id", required = true),
            @Parameter(name = "number", description = "数量", required = true),
            @Parameter(name = "validity", description = "有效期", required = true),
            @Parameter(name = "isPrescription", description = "是否处方药", required = true),
            @Parameter(name = "wDescribe", description = "描述", required = true),
    })
    public CommonResult createCart(@RequestParam(required = true) String position,
                                   @RequestParam(required = true) Integer number,
                                   @RequestParam(required = true) String validity,
                                   @RequestParam(required = true) Integer isPrescription,
                                   @RequestParam(required = true) String wDescribe) {
        drugService.save(new Drug(null,position,number,validity,isPrescription,wDescribe));
        return new CommonResult(200, "创建药物成功");
    }

    //根据id获取药物
    @GetMapping("/id")
    @Operation(summary = "根据id获取药物", description = "() => 获取指定药物")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult getDrugById(@RequestParam Integer id) {
        Drug drug = drugService.getById(id);
        return new CommonResult(200, "根据id获取药物", drug);
    }

    //根据id更新药物信息
    @PutMapping("/id")
    @Operation(summary = "根据id更新药物信息", description = "(Integer id,?String position, ?Integer number, ?String validity, +\n" +
            "            \"                                   ?Integer isPrescription, ?String wDescribe) => 更新药物信息")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "position", description = "id", required = false),
            @Parameter(name = "number", description = "数量", required = false),
            @Parameter(name = "validity", description = "有效期", required = false),
            @Parameter(name = "isPrescription", description = "是否处方药", required = false),
            @Parameter(name = "wDescribe", description = "描述", required = false),
    })
    public CommonResult getCartById(@RequestParam Integer id,
                                    @RequestParam(required = false) String position,
                                    @RequestParam(required = false) Integer number,
                                    @RequestParam(required = false) String validity,
                                    @RequestParam(required = false) Integer isPrescription,
                                    @RequestParam(required = false) String wDescribe) {
        Drug drug = drugService.getById(id);
        if (drug != null) {
            drugService.updateById(new Drug(id,position,number,validity,isPrescription,wDescribe));
            return new CommonResult(200, "根据id:" + id + ",更新药物信息成功");
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到药物");
        }
    }

    //根据id移除药物信息
    @DeleteMapping("/id")
    @Operation(summary = "根据id移除药物信息", description = "(Integer id) => 根据id移除药物信息")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult removeCartById(@RequestParam(required = true) Integer id) {
        Drug drug = drugService.getById(id);
        if (drug != null) {
            drugService.removeById(id);
            return new CommonResult(200, "根据id:" + id + ",移除药物成功");
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到药物");
        }
    }


}
